<?php
class Controller_Profile extends Controller_Template
{
	//Constructor
	public function __construct()
	{
		parent::__construct();
		$this->selfModel = new Model_Profile();
		$this->personModel = new Model_Person();
		$this->carModel = new Model_Car();
		$this->notificationModel = new Model_Notification();
	}
	
	//Methods
	public function index()
	{
		$title = "Profile";
		$requiredField = '<span class="red">*</span> Required field</br>';
		
		require 'View/header.tpl';
		require 'View/menu.tpl';
		require 'View/profile/index.tpl';
		require 'View/footer.tpl';
		//return true;
	}
	public function search($phone, $lastURI)
	{
		$title = "Profile Search";
		$requiredField = '<span class="red">*</span> Required field</br>';
		
		$person = $this->personModel->getPerson($phone);
		if (empty($person))
		{
			if (isset($_SESSION['searchProfilePhone']))
			{
				unset($_SESSION['searchProfilePhone']);
			}
			setcookie("SearchProfile","PersonNotExist");
			header('Location: '.html($lastURI));
		}
		else
		{
			$profile = $this->selfModel->getProfile($person->getPersonId());
			if (empty($profile))
			{
				if (isset($_SESSION['searchProfilePhone']))
				{
					unset($_SESSION['searchProfilePhone']);
				}
				setcookie("SearchProfile","ProfileNotExist");
				header('Location: '.html($lastURI));
			}
			else
			{
				$cars = $this->carModel->getAllCars($profile->getProfileId());
				$notifications=$this->notificationModel->getAllNotifications($profile->getProfileId());
				$notificationType=$this->notificationModel->getAllNotificationTypes();
				
				require 'View/header.tpl';
				require 'View/menu.tpl';
				require 'View/profile/index.tpl';
				require 'View/footer.tpl';
			}
		}

		//return false;
	}
	public function create($id)
	{
		$title = "Profile Create";
		$requiredField = '<span class="red">*</span> Required field</br>';
	
		$person = $this->personModel->getPersonById($id);
		if (empty($person))
		{
			return;
		}
		else
		{
			$profile = $this->selfModel->getProfile($person->getPersonId());
			if (empty($profile))
			{
				return;
			}
			else
			{
				$cars = $this->carModel->getAllCars($profile->getProfileId());
				$notifications=$this->notificationModel->getAllNotifications($profile->getProfileId());
				$notificationType=$this->notificationModel->getAllNotificationTypes();
			}
		}
	
		require 'View/header.tpl';
		require 'View/menu.tpl';
		require 'View/profile/index.tpl';
		require 'View/footer.tpl';
		//return true;
	}
	public function selectCars($index, $phone)
	{
		$title = "Profile Cars";
		$requiredField = '<span class="red">*</span> Required field</br>';
	
		$carIndex = $index;
		$person = $this->personModel->getPerson($phone);
		if (empty($person))
		{
			return;
		}
		else
		{
			$profile = $this->selfModel->getProfile($person->getPersonId());
			if (empty($profile))
			{
				return;
			}
			else
			{
				$cars = $this->carModel->getAllCars($profile->getProfileId());
				$notifications=$this->notificationModel->getAllNotifications($profile->getProfileId());
				$notificationType=$this->notificationModel->getAllNotificationTypes();
			}
		}
	
		require 'View/header.tpl';
		require 'View/menu.tpl';
		require 'View/profile/index.tpl';
		require 'View/footer.tpl';
		//return false;
	}
	
}